<?php


namespace app\common\model;


use think\Model;
use traits\model\SoftDelete;

class Jdz extends Model
{

    use SoftDelete;
    protected $deleteTime = 'delete_time';  //软删除字段,ThinkPHP5的软删除功能使用时间戳类型（数据表默认值为Null）
    protected $type = ['delete_time' => 'datetime'];  //写入和读取数据的时候都会自动处理成时间字符串Y-m-d H:i:s的格式。

    public function _save($data)
    {
        $data = $data ? $data : input();
        //发布提议发送信息给相关部门领导人
        $leader = new Leader();
        $work = new Work();
        $message = new Message();
        $task = new Task();
        $manager = new Manager();
        $user = new User();
        $roule = model('System')->getValue('roule_config');
        $my_department_id = $data['propose_department_id'];
        $my_leader_info = $leader->where(['department_id' => $my_department_id])->find();
        $my_leader_user_list = json_decode($my_leader_info['leader_user_list']);
        $my_class_user_list = json_decode($my_leader_info['class_user_list']);
        $leader_info = $leader->where('department_id', '=', $data['departid'])->find();
        $leader_user_list = json_decode($leader_info['leader_user_list']);
        $class_user_list = json_decode($leader_info['class_user_list']);
        if ($data['status'] != 15) {
            if ($leader_user_list == null) {
                return ajax_return(2, '该部门未设置部门领导，无法进行采纳操作，请联系管理员');
            }
            if ($class_user_list == null) {
                return ajax_return(2, '该部门未设置分管领导，无法进行评估操作，请联系管理员');
            }
            if ($my_leader_user_list == null) {
                return ajax_return(2, '您所属部门未设置部门领导，无法进行采纳操作，请联系管理员');
            }
            if ($my_class_user_list == null) {
                return ajax_return(2, '您所属部门未设置分管领导，无法进行评估操作，请联系管理员');
            }
        }
        if ($data['status'] == 4) {
            $data['reward_to'] = 1;
        }
        $res = $this->allowField(true)->save($data, $data['id'] ? $data['id'] : '');
        //发布话题。推送消息给我的部门领导审核 15
        if (in_array($data['status'], [15])) {
            foreach ($my_leader_user_list as $v) {
                $message->send_message($v, '您部门成员有新的提议需要审核，请前往金点子模块进行查看。提议名称:' . $data["title"], '3', url('jdz/detail', ['id' => $this->id]));
            }
            $work->send_templete_message($my_leader_user_list, '审核通知', '您部门成员发布了新的提交，需要您进行审核，请前往处理。提议名称:' . $data['title'], site_url . '/index/jdz/detail?id=' . $this->id);
        }
        //复活话题。推送消息给我的部门领导审核 17
        if (in_array($data['status'], [17])) {
            foreach ($my_class_user_list as $v) {
                $message->send_message($v, '您分管部门成员有新的提议需要审核，请前往金点子模块进行查看。提议名称:' . $data["title"], '3', url('jdz/detail', ['id' => $this->id]));
            }
            $work->send_templete_message($my_class_user_list, '审核通知', '您分管部门成员发布了新的提交，需要您进行审核，请前往处理', site_url . '/index/jdz/detail?id=' . $this->id);
        }

        //提议被提出部门领导通过 18
        if (in_array($data['status'], [18])) {
            foreach ($my_class_user_list as $v) {
                $message->send_message($v, '您分管部门成员有新的提议需要评估，请前往金点子模块进行查看。提议名称:' . $data['title'], '3', url('jdz/detail', ['id' => $this->id]));
            }
            $work->send_templete_message($my_class_user_list, '审核通知', '您分管部门成员发布了新的提议，需要您进行评估，请前往处理。提议名称:' . $data['title'], site_url . '/index/jdz/detail?id=' . $this->id);
        }
        //话题/补充话题，发消息给部门经理 1 9
        if (in_array($data['status'], [1, 9])) {
            foreach ($leader_user_list as $v) {
                $message->send_message($v, '您有新的提议需要审核，请前往金点子模块进行查看。提议名称:' . $data['title'], '3', url('jdz/detail', ['id' => $this->id]));
            }
            $work->send_templete_message($leader_user_list, '审核通知', '有新的提议提交了，需要您进行审核，请前往处理。提议名称:' . $data['title'], site_url . '/index/jdz/detail?id=' . $this->id);
        }
        //第一次复活的话题，发消息给分管领导 5
        if (in_array($data['status'], [5])) {
            foreach ($class_user_list as $v) {
                $message->send_message($v, '您有新的提议需要审核，请前往金点子模块进行查看。提议名称:' . $data['title'], '3', url('jdz/detail', ['id' => $this->id]));
            }
            $work->send_templete_message($class_user_list, '审核通知', '有新的提议提交了，需要您进行审核，请前往处理。提议名称:' . $data['title'], site_url . '/index/jdz/detail?id=' . $this->id);
        }
        //采纳的话题(包括补充的话题)，发消息给分管领导 2 11
        if (in_array($data['status'], [2, 11])) {
            foreach ($class_user_list as $v) {
                $message->send_message($v, '您有新的提议需要评估，请前往金点子模块进行查看。提议名称:' . $data['title'], '3', url('jdz/detail', ['id' => $this->id]));
            }
            $work->send_templete_message($class_user_list, '评估通知', '您有新的提议需要评估，请前往金点子模块进行查看。提议名称:' . $data['title'], site_url . '/index/jdz/detail?id=' . $this->id);
        }
        //新发布/复活话题未被采纳，发消息给提议人 3 6 8
        if (in_array($data['status'], [3, 6, 8, 16])) {
            $message->send_message($data['user_id'], '您的提议未被采纳/评估，请前往金点子模块查看。提议名称:' . $data['title'], '3', url('jdz/detail', ['id' => $this->id]), '1');
            $work->send_templete_message([$data['user_id']], '审核通知', '您的提议未被采纳/评估，请前往金点子模块查看。提议名称:' . $data['title'], site_url . '/index/jdz/detail?id=' . $this->id);
        }
        //再次申请复活发消息给总经理 7
        if (in_array($data['status'], [7])) {
            $managers = $manager->find(1);
            $manager = json_decode($managers['list'], true);
            foreach ($manager as $v) {
                $message->send_message($v, '您有新的提议需要评估，请前往金点子模块进行查看。提议名称:' . $data['title'], '3', url('jdz/detail', ['id' => $this->id]));
            }
            $work->send_templete_message($manager, '审核通知', '您有新的提议需要评估，请前往金点子模块进行查看。提议名称:' . $data['title'], site_url . '/index/jdz/detail?id=' . $this->id);
        }
        //补充的话题未被采纳  发消息给补充人10
        if (in_array($data['status'], [7])) {
            $message->send_message($data['supp_user'], '您补充的提议未被审核通过。提议名称:' . $data['title'], '3', url('jdz/detail', ['id' => $this->id]));
            $work->send_templete_message([$data['supp_user']], '审核通知', '您补充的提议未被审核通过。提议名称:' . $data['title'], site_url . '/index/jdz/detail?id=' . $this->id);
        }
        //评估后，发消息给完成人员、合作伙伴、提交人 4 12 发给补充人
        if (in_array($data['status'], [4, 12])) {
            $complete_user_id = explode(',', $data['complete_user_id']);
            $partners = explode(',', $data['partners_id']);
            foreach ($complete_user_id as $v) {
                $message->send_message($v, '有新的提议/补充和您相关，请前往金点子模块确认。提议名称:' . $data['title'], 3, url('jdz/detail', ['id' => $this->id]));
            }
            $work->send_templete_message($complete_user_id, '提议确认', '有新的提议/补充和您相关，请前往金点子模块确认。提议名称:' . $data['title'], site_url . '/index/jdz/detail?id=' . $this->id);
            foreach ($partners as $v) {
                $message->send_message($v, '有新的提议/补充和您相关，请前往金点子模块确认。提议名称:' . $data['title'], 3, url('jdz/detail', ['id' => $this->id]));
            }
            $work->send_templete_message($partners, '提议确认', '有新的提议/补充和您相关，请前往金点子模块确认。提议名称:' . $data['title'], site_url . '/index/jdz/detail?id=' . $this->id);
            $message->send_message($data['user_id'], '您的提议已经评估完成，请前往金点子模块查看。提议名称:' . $data['title'], '3', url('jdz/detail', ['id' => $this->id]), '1');
            $work->send_templete_message([$data['user_id']], '评估通知', '您的提议已经评估完成，请前往金点子模块查看。提议名称:' . $data['title'], site_url . '/index/jdz/detail?id=' . $this->id);
            $user->put_integral(4, $roule['jdz_integral'], $this->id, $data['user_id']);
            if ($data['complete_type'] == 3) {
                $user_list = $user->column('user_id');
                foreach ($user_list as $v) {
                    $message->send_message($v, '有新的提议已经审核通过了，请前往查看，揭榜并完成后会有奖励', 3, url('jdz/detail', ['id' => $this->id]));
                }
                $work->send_templete_message($user_list, '揭榜提醒', '有新的提议已经审核通过了，请前往查看，揭榜并完成后会有奖励' . $data['title'], site_url . '/index/jdz/detail?id=' . $this->id);
            }
            //
            if ($data['reward_to'] == 1) {
                $reward = new Reward();
                $reward_info = $reward->where(['jdz_id'=>$this->id])->find();
                if(!$reward_info){
                    $save_reward_data['jdz_id']=$this->id;
                    $save_reward_data['user_id']=$data['user_id'];
                    $save_reward_data['status']=0;
                    $reward->insert($save_reward_data);
                }
                $acceptor = $manager->find(3);
                $acceptors = json_decode($acceptor['list'], true);
                foreach ($acceptors as $v) {
                    $message->send_message($v, '有新的提议被审核通过了，需要进行红包发放，请前往查看。提议名称:' . $data['title'], 3, url('reward/detail', ['jdz_id' => $this->id]));
                }
                $work->send_templete_message($acceptors, '红包发放通知', '有新的提议被审核通过了，需要进行红包发放，请前往查看。提议名称:' . $data['title'], site_url . '/index/reward/detail?jdz_id=' . $this->id);
            }
            /*
            if ($data['status'] == 12) {
                $message->send_message($data['supp_user'], '您补充的提议审核通过。提议名称:' . $data['title'], '3', url('jdz/detail', ['id' => $this->id]));
                $work->send_templete_message([$data['supp_user']], '审核通知', '您补充的提议审核通过。提议名称:' . $data['title'], site_url . '/index/jdz/detail?id=' . $this->id);
            }
            */
        }
        //完成后发消息给提交人、完成人、评分人(部门领导+分管领导) 13
        if (in_array($data['status'], [13])) {
            $score = new Score();
            $score_list = $score->where(['jdz_id' => $data['id']])->find();
            if (!$score_list) {
                $message->send_message($data['user_id'], '您的提议已经完成，请前往金点子模块查看。提议名称:' . $data['title'], '3', url('jdz/detail', ['id' => $this->id]), '1');
                $work->send_templete_message([$data['user_id']], '完成通知', '您的提议已经完成，请前往金点子模块查看。提议名称:' . $data['title'], site_url . '/index/jdz/detail?id=' . $this->id);
                $complete_user = $task->where(['jdz_id' => $data['id'], 'status' => 2])->column('user_id');
                foreach ($complete_user as $v) {
                    $message->send_message($v, '您参与的提议已经完成，等待验收。提议名称:' . $data['title'], 3, url('jdz/detail', ['id' => $this->id]));
                }
                $work->send_templete_message($complete_user, '完成通知', '您参与的提议已经完成，等待验收。提议名称:' . $data['title'], site_url . '/index/jdz/detail?id=' . $this->id);
                //验收人
                // $acceptor = $manager->find(1);
                // $acceptors = json_decode($acceptor['list'], true);
                foreach ($leader_user_list as $v) {
                    $message->send_message($v, '您有一个新提议需要验收。提议名称:' . $data['title'], 3, url('jdz/detail', ['id' => $this->id]));
                }
                $work->send_templete_message($leader_user_list, '验收通知', '您有一个新提议需要验收。提议名称:' . $data['title'], site_url . '/index/jdz/detail?id=' . $this->id);
                foreach ($class_user_list as $v) {
                    $message->send_message($v, '您有一个新提议需要验收。提议名称:' . $data['title'], 3, url('jdz/detail', ['id' => $this->id]));
                }
                $work->send_templete_message($class_user_list, '验收通知', '您有一个新提议需要验收。提议名称:' . $data['title'], site_url . '/index/jdz/detail?id=' . $this->id);

            }
        }
        //验收后发消息给部门经理、完成人、提议人 14
        if (in_array($data['status'], [14])) {
            if ($data['score_flag'] == 0) {

                $message->send_message($data['user_id'], '您的提议已经验收完成，请前往金点子模块查看。提议名称:' . $data['title'], '3', url('jdz/detail', ['id' => $this->id]), '1');
                $work->send_templete_message([$data['user_id']], '验收通知', '您的提议验收完成，请前往金点子模块查看。提议名称:' . $data['title'], site_url . '/index/jdz/detail?id=' . $this->id);
                $complete_user = $task->where(['jdz_id' => $data['id'], 'status' => 2])->column('user_id');
                foreach ($complete_user as $v) {
                    $message->send_message($v, '您参与的提议已经验收完成。提议名称:' . $data['title'], 3, url('jdz/detail', ['id' => $this->id]));
                    $user->put_integral(6, $roule['finish_integral'], $this->id, $v);
                }
                $work->send_templete_message($complete_user, '验收通知', '您参与的提议已经验收完成。提议名称:' . $data['title'], site_url . '/index/jdz/detail?id=' . $this->id);

                $data['score_flag'] = 1;
                $this->save($data, ['id' => $data['id']]);
            }
        }
        //全部完成后，发消息给提议人确认
        if (in_array($data['status'], [19])) {
            $message->send_message($data['user_id'], '您的提议已经已经完成，请前往金点子模块查看。提议名称:' . $data['title'], '3', url('jdz/detail', ['id' => $this->id]), '1');
            $work->send_templete_message([$data['user_id']], '完成通知', '您的提议已经完成，请前往金点子模块查看。提议名称:' . $data['title'], site_url . '/index/jdz/detail?id=' . $this->id);
        }
        //自该自提的保存任务
        if ((in_array($data['status'], [4, 10, 12])) && $data['complete_type'] == 2) {
            //halt($task->where(['user_id' => $data['user_id'], 'jdz_id' => $data['id']])->find());
            if (!($task->where(['user_id' => $data['user_id'], 'jdz_id' => $data['id']])->find())) {
                $t['jdz_id'] = $data['id'];
                $t['user_id'] = $data['user_id'];
                $t['content'] = '';
                $t['add_time'] = date('Y-m-d H:i:s');
                $t['status'] = 1;
                $task->save($t);
            }
        }
        //自该自提的保存任务
        if ((in_array($data['status'], [4, 10, 12])) && $data['complete_type'] == 1) {
            $complete_user_id = explode(',', $data['complete_user_id']);
            foreach ($complete_user_id as $v) {
                if (!($task->where(['user_id' => $v, 'jdz_id' => $data['id']])->find())) {
                    $t['jdz_id'] = $data['id'];
                    $t['user_id'] = $v;
                    $t['content'] = '';
                    $t['add_time'] = date('Y-m-d H:i:s');
                    $t['status'] = 1;
                    $task->insert($t);
                }
            }
        }
        if ($res !== false) {
            return ajax_return(1, '成功！');
        } else {
            return ajax_return(0, $this->getError());
        }
    }

    public function getLists($query)
    {
        $data = input('param.');
        $limit = input('limit');
        $order = input('order') ? input('order') : 'appreciatenumber desc,add_time desc';
        $data['title'] ? $where['title'] = ['like', '%' . $data['title'] . '%'] : '';
        $data['status'] ? $where['status'] = ['in', $data['status']] : '';
        $list = $this->where($where)->where($query)->order($order)->paginate($limit)->toArray();
        $list['code'] = 0;//用于前台数据表格判断成功字段
        return $list;
    }

    public function getStatusTextAttr($value, $data)
    {
        $status = [
            1 => '已提交',
            2 => '已采纳',
            3 => '未采纳',
            4 => '待完成',
            5 => '已申请复活',
            6 => '未采纳',
            7 => '再次申请复活',
            8 => '总经理未采纳',
            9 => '新补充（待采纳）',
            10 => '新补充（未采纳）',
            11 => '新补充（已采纳）',
            12 => '新补充（以评估）',
            13 => '已完成',
            14 => '已验收(已关闭)',
            15 => '发布成功',
            16 => '未采纳',
            17 => '已申请复活',
            18 => '已采纳',
            19 => '任务完成',
            20 => '提议被撤回(已关闭)'
        ];
        $status = [
            1 => '问题部门领导审核',
            2 => '问题部门领导评估',
            3 => '未采纳',
            4 => '待完成',
            5 => '问题部门领导审核',
            6 => '未采纳',
            7 => '再次申请复活',
            8 => '总经理未采纳',
            9 => '新补充（待采纳）',
            10 => '新补充（未采纳）',
            11 => '新补充（已采纳）',
            12 => '新补充（以评估）',
            13 => '已完成',
            14 => '已验收(已关闭)',
            15 => '发布成功',
            16 => '未采纳',
            17 => '提议部门分管领导审核',
            18 => '提议部门分管领导评估',
            19 => '任务完成',
            20 => '提议被撤回(已关闭)'
        ];
        return $status[$data['status']];
    }

    public function getnextstatusTextAttr($value, $data)
    {
        $nextstatus = [
            1 => '问题部门领导审核',
            2 => '问题部门分管领导评估',
            3 => '--',
            4 => '待验收',
            5 => '待分管领导采纳',
            6 => '--',
            7 => '待总经理审核',
            8 => '--',
            9 => '待问题部门领导审核',
            10 => '--',
            11 => '待问题部门分管领导评估',
            12 => '待完成',
            13 => '待验收',
            14 => '--',
            15 => '待提出部门领导审核',
            16 => '--',
            17 => '待提出部门分管领导审核',
            18 => '待提出部门分管领导审核',
            19 => '待完成确认',
            20 => '任务关闭'
        ];
        return $nextstatus[$data['status']];
    }

    public function get_finish_status($id)
    {
        $task = new Task();
        $list = $task->where(['jdz_id' => $id, 'status' => 1, 'complete_content' => ''])->find();
        if ($list) {
            return false;
        }
        return true;
    }

}